package com.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;

import com.dao.CommonDAO;
import com.dao.SysuserDAO;
import com.model.Familycy;
import com.model.Familyda;
import com.model.Notice;
import com.model.Sysuser;
import com.model.Tzdata;
import com.opensymphony.xwork2.ActionSupport;
import com.util.Info;
import com.util.Pagination;

public class TzdataAction extends ActionSupport
{
	private Integer id;
	private String tw;
	private String mb;
	private String xy;
	private String cdl;
	private String dz;
	private String hx;
	private String cyid;
	private String recorddate;
	private String recordtime;
	private int index=1;
	String no;
	String suc;
	private String utype;
	private CommonDAO commonDAO;
	private ArrayList<Tzdata> rslist;
	private String xml;
	
	
	public String getXml() {
		return xml;
	}
	public void setXml(String xml) {
		this.xml = xml;
	}
	public ArrayList<Tzdata> getRslist() {
		return rslist;
	}
	public void setRslist(ArrayList<Tzdata> rslist) {
		this.rslist = rslist;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getTw() {
		return tw;
	}
	public void setTw(String tw) {
		this.tw = tw;
	}
	public String getMb() {
		return mb;
	}
	public void setMb(String mb) {
		this.mb = mb;
	}
	public String getXy() {
		return xy;
	}
	public void setXy(String xy) {
		this.xy = xy;
	}
	public String getCdl() {
		return cdl;
	}
	public void setCdl(String cdl) {
		this.cdl = cdl;
	}
	public String getDz() {
		return dz;
	}
	public void setDz(String dz) {
		this.dz = dz;
	}
	public String getHx() {
		return hx;
	}
	public void setHx(String hx) {
		this.hx = hx;
	}
	public String getCyid() {
		return cyid;
	}
	public void setCyid(String cyid) {
		this.cyid = cyid;
	}
	public String getRecorddate() {
		return recorddate;
	}
	public void setRecorddate(String recorddate) {
		this.recorddate = recorddate;
	}
	public String getRecordtime() {
		return recordtime;
	}
	public void setRecordtime(String recordtime) {
		this.recordtime = recordtime;
	}
	public int getIndex() {
		return index;
	}
	public void setIndex(int index) {
		this.index = index;
	}
	public String getNo() {
		return no;
	}
	public void setNo(String no) {
		this.no = no;
	}
	public String getSuc() {
		return suc;
	}
	public void setSuc(String suc) {
		this.suc = suc;
	}
	public String getUtype() {
		return utype;
	}
	public void setUtype(String utype) {
		this.utype = utype;
	}
	public CommonDAO getCommonDAO() {
		return commonDAO;
	}
	public void setCommonDAO(CommonDAO commonDAO) {
		this.commonDAO = commonDAO;
	}
	//添加
	public String TzdataAdd(){
		Tzdata tzdata = new Tzdata();
		tzdata.setTw(tw);
		tzdata.setMb(mb);
		tzdata.setXy(xy);
		tzdata.setCdl(cdl);
		tzdata.setDz(dz);
		tzdata.setHx(hx);
		tzdata.setRecorddate(recorddate);
		tzdata.setRecordtime(recordtime);
		tzdata.setCyid(cyid);
		  		commonDAO.save(tzdata);
				suc="";
			return "success";
	}
	
	//添加
	public void tzadd(){
		Tzdata tzdata = new Tzdata();
		tzdata.setTw(tw);
		tzdata.setMb(mb);
		tzdata.setXy(xy);
		tzdata.setCdl(cdl);
		tzdata.setDz(dz);
		tzdata.setHx(hx);
		tzdata.setRecorddate(recorddate);
		tzdata.setRecordtime(recordtime);
		tzdata.setCyid(cyid);
		commonDAO.save(tzdata);
				
	}
	
	public void searchTz() throws IOException{
		String hql = " from Tzdata where cyid='"+cyid+"' and recorddate='"+Info.getDateStr().substring(0,10)+"' order by recordtime";
		rslist = (ArrayList<Tzdata>)commonDAO.findByHql(hql);
		String xml = "<graph showNames='1' animation='0' foceDecimals='1' formatNumberScale='0' >";
		xml+=" <categories>";
		for(Tzdata tz:rslist){
			String time = tz.getRecordtime();
			xml += "<category name='"+time+"'/>";
		}
		xml+="</categories>";
		xml+="<dataset seriesName='体温' color='F1683C' anchorBorderColor='F1683C' anchorBgColor='F1683C'>";
		for(Tzdata tz:rslist){
			Double tw = Double.valueOf(tz.getTw());
			xml+=" <set value='"+tw+"'/>";
		}
		xml+="</dataset>";
		
		xml+="<dataset seriesName='脉搏' color='1D8BD1' anchorBorderColor='1D8BD1' anchorBgColor='1D8BD1'>";
		for(Tzdata tz:rslist){
			Double mb = Double.valueOf(tz.getMb());
			xml+=" <set value='"+mb+"'/>";
		}
		xml+="</dataset>";
		
		xml+="<dataset seriesName='血氧' color='8BBA00' anchorBorderColor='8BBA00' anchorBgColor='8BBA00'>";
		for(Tzdata tz:rslist){
			Double xy = Double.valueOf(tz.getXy());
			xml+=" <set value='"+xy+"'/>";
		}
		xml+="</dataset>";
		
		xml+="<dataset seriesName='传导率' color='FF8E46' anchorBorderColor='FF8E46' anchorBgColor='FF8E46'>";
		for(Tzdata tz:rslist){
			Double cdl = Double.valueOf(tz.getCdl());
			xml+=" <set value='"+cdl+"'/>";
		}
		xml+="</dataset>";
		
		xml+="<dataset seriesName='电阻' color='008E8E' anchorBorderColor='008E8E' anchorBgColor='008E8E'>";
		for(Tzdata tz:rslist){
			Double dz = Double.valueOf(tz.getDz());
			xml+=" <set value='"+dz+"'/>";
		}
		xml+="</dataset>";
		
		xml+="<dataset seriesName='呼吸流量' color='2AD62A' anchorBorderColor='2AD62A' anchorBgColor='2AD62A'>";
		for(Tzdata tz:rslist){
			Double hx = Double.valueOf(tz.getHx());
			xml+=" <set value='"+hx+"'/>";
		}
		xml+="</dataset>";
		
		xml+="</graph>";  
		
		String responseContext="";
		responseContext = xml;
		HttpServletResponse response=ServletActionContext.getResponse();
 		response.setContentType("text/html");
 		response.setCharacterEncoding("utf-8"); 
 		PrintWriter out = response.getWriter();
 		out.println(responseContext);
 		out.flush();
 		out.close();
	}
	
	public String  test() throws IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		try {
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 获取实际电量和计划电量
		List xAxisData = new ArrayList(); 
		List twlist = new ArrayList();
		List mblist = new ArrayList();
		List xylist = new ArrayList();
		List cdllist = new ArrayList();
		List dzlist = new ArrayList();
		List hxlist = new ArrayList();
		String hql = " from Tzdata where cyid='"+cyid+"' and recorddate='"+Info.getDateStr().substring(0,10)+"' order by recordtime";
		rslist = (ArrayList<Tzdata>)commonDAO.findByHql(hql);
		for (Tzdata tz : rslist) {
			xAxisData.add(tz.getRecordtime());
			twlist.add(tz.getTw());
			mblist.add(tz.getMb());
			xylist.add(tz.getXy());
			cdllist.add(tz.getCdl());
			dzlist.add(tz.getDz());
			hxlist.add(tz.getHx());
		}
		List<List> merge = new ArrayList();
		merge.add(xAxisData);
		merge.add(twlist);
		merge.add(mblist);
		merge.add(xylist);
		merge.add(cdllist);
		merge.add(dzlist);
		merge.add(hxlist);
		JSONArray jsonArray = JSONArray.fromObject(merge);
		try {
			response.setHeader("Cache-Control", "no-cache");
			response.setContentType("aplication/json;charset=UTF-8");
			response.getWriter().print(jsonArray);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// request.setAttribute("list_powers", powers);
		// return "success";
		return null;
	}
	
}
